package com.ccloud.im.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.ccloud.common.core.utils.ValueUtils;
import com.ccloud.common.im.model.CIMSession;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * Im 实时在线用户信息表
 * </p>
 *
 * @author chentl123
 * @since 2020-09-07
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("im_user_session")
public class UserSession extends Model<UserSession> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    /**
     * 用户ID
     */
    private Integer userId;
    /**
     * 用户名称
     */
    private String account;
    /**
     * nio id
     */
    private String nid;
    /**
     * 服务器地址
     */
    private String host;
    /**
     * 终端设备类型
     */
    private String channel;
    /**
     * 终端设备型号
     */
    private String device;
    /**
     * 终端版本号
     */
    private String osVersion;
    /**
     * 登录时间
     */
    private Date bindTime;

    public static UserSession convert(CIMSession cimSession) {
        UserSession userSession = new UserSession();

        userSession.setAccount(cimSession.getUserId());
        userSession.setUserId(ValueUtils.parseInteger(cimSession.getUserId()));
        userSession.setBindTime(new Date(cimSession.getBindTime()));
        userSession.setChannel(cimSession.getChannel());
        userSession.setDevice(cimSession.getDevice());
        userSession.setOsVersion(cimSession.getOsVersion());
        userSession.setNid(cimSession.getNid());
        userSession.setHost(cimSession.getHost());
        return userSession;
    }

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}
